package com.xiaoniu.mc.util;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @类描述：序列号生成器
 * @创建人：林继丰
 * @创建时间：2017年3月23日
 * @版权：Copyright (c) 深圳市牛鼎丰科技有限公司-版权所有.
 */
public final class SerialNumberUtil {

    public enum ShearStrategy {RETAIN_TOP, RETAIN_BUTTOM}

    ;

    public static final String[] numAndLetter = {"0", "1", "2", "3", "4", "5", "6", "7", "8",
            "9", "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G", "h",
            "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p",
            "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "x",
            "X", "y", "Y", "z", "Z"};

    public static final String[] numAndLetterStr = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    public static final String[] numStr = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

    public static final String[] num4Str = {"1", "2", "3", "4"};


    private SerialNumberUtil() {
        throw new RuntimeException("util class is static");
    }

    /**
     * 得到当前时间(long)的字符串，13位
     *
     * @param time
     * @return
     */
    public static String getTimeString13(long time) {
        String currentTimeS = String.valueOf(time);
        int beginIndex = currentTimeS.length() > 13 ? currentTimeS.length() - 13 : 0;
        return currentTimeS.substring(beginIndex);
    }

    /**
     * 得到随机的字符串，包含大小写字母和数字
     *
     * @param length 返回字符串的长度
     * @return
     */
    public static String getRandomString(int length) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int index = (int) (ThreadLocalRandom.current().nextDouble() * 62);
            buffer.append(numAndLetter[index]);
        }
        return buffer.toString();
    }

    /**
     * 得到随机的字符串，包含大小写字母和数字
     *
     * @param length 返回字符串的长度
     * @return
     */
    public static String getRandomStr(int length) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int index = (int) (ThreadLocalRandom.current().nextDouble() * 26);
            buffer.append(numAndLetterStr[index].toUpperCase());
        }
        return buffer.toString();
    }


    /**
     * 得到指定位数的纯数字随机码
     *
     * @param length 位数
     * @return 返回字符串
     */
    public static String getDigitRandom(int length) {
        StringBuffer buffer = new StringBuffer();
        int range = 10;
        for (int i = 0; i < length; i++) {
            int index = (int) (ThreadLocalRandom.current().nextDouble() * range);
            buffer.append(numStr[index]);
        }
        return buffer.toString();
    }

    /**
     * 得到指定位数的纯数字随机码
     *
     * @param length 位数
     * @return 返回字符串
     */
    public static String getDigit4Random(int length) {
        StringBuffer buffer = new StringBuffer();
        int range = 4;
        for (int i = 0; i < length; i++) {
            int index = (int) (ThreadLocalRandom.current().nextDouble() * range);
            buffer.append(num4Str[index]);
        }
        return buffer.toString();
    }

}
